package com.yunji.base.business;

import com.yunji.base.bo.StaffLocationRemoveBO;
import com.yunji.base.consumer.BaseDataConsumer;
import com.yunji.base.cache.BaseCache;
import com.yunji.base.convert.SiteLocationConvert;
import com.yunji.base.domain.BelongDO;
import com.yunji.base.domain.StaffLocationDO;
import com.yunji.base.dto.DeployStaffDTO;
import com.yunji.base.dto.SiteLocationDTO;
import com.yunji.base.params.SiteLocationParam;
import com.yunji.base.service.SiteLocationService;
import com.yunji.base.service.StaffLocationService;
import com.yunji.base.vo.DeployStaffVO;
import com.yunji.base.vo.SiteLocationVO;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.system.context.SystemContext;
import io.jsonwebtoken.lang.Assert;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class SiteLocationBusiness {

    @Resource
    private SiteLocationService siteLocationService;

    @Resource
    private BaseDataConsumer baseDataConsumer;

    @Resource
    private StaffLocationService staffLocationService;

    @Transactional
    public Long create(SiteLocationDTO dto) {
        LoginInfo user = SystemContext.getUser();
        if (UserType.SITE.getCode().equals(user.getUserType())){
            BelongDO belong = BaseCache.getBelong(user.getBelongId());
            dto.setSiteId(belong.getSiteId());
        }
        BelongDO belong = BaseCache.getBelongBySiteId(dto.getSiteId());
        dto.setAgentId(belong.getAgentId());
        dto.setBelongId(belong.getBelongId());
        return siteLocationService.create(SiteLocationConvert.DTOToDO(dto));
    }

    public SiteLocationVO getInfo(Long id) {
        return siteLocationService.getInfo(id);
    }

    public void update(SiteLocationDTO dto) {
        if (dto.getSiteId() != null){
            BelongDO belong = BaseCache.getBelongBySiteId(dto.getSiteId());
            dto.setBelongId(belong.getBelongId());
        }
        siteLocationService.update(SiteLocationConvert.DTOToDO(dto));
    }

    public void delete(Long... ids) {
        Assert.isTrue(!baseDataConsumer.containerExistsSiteLocation(Arrays.asList(ids)), ErrorEnum.BUSINESS_712007.getText());
        Assert.isTrue(!staffLocationService.existsLocation(Arrays.asList(ids)),ErrorEnum.BUSINESS_612006.getText());
        siteLocationService.delete(Arrays.asList(ids));
    }

    public void deployStaff(DeployStaffDTO dto) {
        Long locationId = dto.getLocationId();
        staffLocationService.delete(StaffLocationRemoveBO.builder().locationId(locationId).build());

        List<Long> staffIds = dto.getStaffIds();
        List<StaffLocationDO> createList = new ArrayList<>();
        for (Long staffId : staffIds) {
            StaffLocationDO staffLocationDO = new StaffLocationDO();
            staffLocationDO.setStaffId(staffId);
            staffLocationDO.setLocationId(locationId);
            createList.add(staffLocationDO);
        }

        staffLocationService.batchCreate(createList);
    }

    public List<DeployStaffVO> getDeployStaffIds(Long locationId) {
        List<DeployStaffVO> voList = new ArrayList<>();
        List<Long> staffIds = staffLocationService.getDeployStaffIds(locationId);
        for (Long staffId : staffIds) {
            DeployStaffVO vo = new DeployStaffVO();
            vo.setStaffId(staffId);
            vo.setStaffName(BaseCache.getStaffName(staffId));
            voList.add(vo);
        }
        return voList;
    }

    public List<SiteLocationVO> getLocationList(SiteLocationParam param) {
        param.setBelongId(SystemContext.getBelongId());
        return siteLocationService.getLocationPageList(param);
    }
}
